package com.u2b.db.datasource;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;



import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import com.sina.sae.util.SaeUserInfo;


/*
 * 确保JDBCUtils是单例的,但是dataSourceWrite原先的用静态变量总是出现空指针，不知道为什么.
 */
public class DbcpHandler
{
    private static DbcpHandler instance = new DbcpHandler();

    private  BasicDataSource dataSourceRead,dataSourceWrite;
   
    
    private DbcpHandler()
    {  
        try
        {
        	dataSourceWrite=createConnectionPool("jdbc:mysql://w.rdc.sae.sina.com.cn:3307/app_aweek");
        	dataSourceRead=createConnectionPool("jdbc:mysql://r.rdc.sae.sina.com.cn:3307/app_aweek");
        }
        catch (Exception e)
        {

            throw new RuntimeException("本地数据初始化连接池错误", e);
        }

    }

    // 需要同步吗,不需要
    public static  DbcpHandler getInstance()
    {
    	
        return instance;

    }

    public synchronized Connection getReadConnection() throws Exception
    {
   
        return dataSourceRead.getConnection();

    }

    public synchronized Connection getWriteConnection() throws Exception
    {
    	
        return dataSourceWrite.getConnection();

    }

    // 要不要同步呢?由于对静态变量进行了操作,需要同步吗?需要.如果操纵的对象只有一个,那么如果是原子操作,就不会出现线程不安全

    // free应该不需要原子操作
    public static void free(Connection conn, Statement st, ResultSet rt)
    {
        try
        {
            if (rt != null)
                rt.close();
        }
        catch (SQLException e)
        {

            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (st != null)
                    st.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
            finally
            {
                if (conn != null)
                {
                    try
                    {
                        conn.close();
                    }
                    catch (SQLException e)
                    {
                        e.printStackTrace();
                    }

                }

            }
        }

    }
 private static   BasicDataSource  createConnectionPool(String url) throws Exception{
	 Properties p = new Properties(); 
 	p.setProperty("driverClassName", "com.mysql.jdbc.Driver"); 
 	p.setProperty("url", url); 
 	p.setProperty("password", SaeUserInfo.getSecretKey()); 
 	p.setProperty("username", SaeUserInfo.getAccessKey()); 
 	p.setProperty("maxActive", "30"); 
 	p.setProperty("maxIdle", "10"); 
 	p.setProperty("maxWait", "1000"); 
 	p.setProperty("removeAbandoned", "true"); 
 	p.setProperty("removeAbandonedTimeout", "120"); 
 	p.setProperty("connectionProperties", "useUnicode=true;characterEncoding=utf-8");
    return (BasicDataSource) BasicDataSourceFactory.createDataSource(p);
	 
	 
 }
     
}
